package com.example.gildestudy1;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;


/**
 * Created by yuwei on 2016/11/24.
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context context;
        private List<String> lists;
        private LayoutInflater layoutInflater;

        private static final int TYPE_ITEM = 0;  //普通Item View

        private static final int TYPE_FOOTER = 1;  //顶部FootView

        //上拉加载更多
        public static final int PULLUP_LOAD_MORE = 0;
        //正在加载中
        public static final int LOADING_MORE = 1;
        //上拉加载更多状态-默认为0
        private int load_more_status = 0;

    public MyAdapter(Context context, List<String> lists) {
        this.context = context;
        this.lists = lists;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //进行判断显示类型，来创建返回不同的View
        if (viewType == TYPE_ITEM) {
            View view = layoutInflater.inflate(R.layout.simple_recycleview, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            MyViewHolder viewHolder = new MyViewHolder(view);
            return viewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View view = layoutInflater.inflate(R.layout.footer_recycleview, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            MyViewHolderFooter viewHolder = new MyViewHolderFooter(view);
            return viewHolder;
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            Glide.with(context).load(lists.get(position)).placeholder(R.mipmap.ic_launcher).error(R.mipmap.error123).crossFade(1000)
                    .into( ((MyViewHolder) holder).imageView);
            //占位符和出错占位符
            //crossFade(int)加入延迟时间

            holder.itemView.setTag(position);
        } else if (holder instanceof MyViewHolderFooter) {
            MyViewHolderFooter footViewHolder = (MyViewHolderFooter) holder;
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footViewHolder.textView.setVisibility(View.VISIBLE);
                    footViewHolder.textView.setText("上拉加载更多...");
                    footViewHolder.progressBar.setVisibility(View.GONE);
                    break;
                case LOADING_MORE:
                    footViewHolder.textView.setText("正在加载...");
//                    footViewHolder.textView.setVisibility(View.GONE);
                    footViewHolder.progressBar.setVisibility(View.VISIBLE);
                    break;
            }
        }
//        System.out.println(lists.get(position));

    }

    @Override
    public int getItemCount() {
        return lists.size() + 1;
    }

    public void add(List<String> s) {
        lists.removeAll(lists);
        lists.addAll(s);
        //   notifyItemRangeChanged(0,s.size());
        notifyDataSetChanged();
    }

    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public void changeMoreStatus(int status){
        load_more_status=status;
        notifyDataSetChanged();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;

    public MyViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.im);
    }
}

class MyViewHolderFooter extends RecyclerView.ViewHolder {
    TextView textView;
    ProgressBar progressBar;
    public MyViewHolderFooter(View itemView) {
        super(itemView);
        progressBar = (ProgressBar)itemView.findViewById(R.id.progress);
        textView = (TextView) itemView.findViewById(R.id.it);
    }
}


